<template>
  <div>
    <elm-page-tb>
      <template #top>
        <elm-row>
          <el-input placeholder="销售批次" size="mini" prefix-icon='el-icon-search' style="margin: 0 10px" v-model="postData.name" clearable></el-input>
          <elm-select placeholder="货物名称" size="mini" v-model="postData.dict" style="margin: 0 10px" url="/business/jxc/goods/dict/GetData" clearable></elm-select>
          <el-date-picker placeholder="起始日期" type="date" value-format="yyyy-MM-dd" size="mini" style="margin: 0 10px" v-model="postData.startDate" clearable></el-date-picker>
          <el-date-picker placeholder="结束日期" type="date" value-format="yyyy-MM-dd" size="mini" style="margin: 0 10px" v-model="postData.endDate" clearable></el-date-picker>
          <right>
            <el-button-group>
              <el-button icon="el-icon-search" size="mini" @click="getTableData()">查询</el-button>
            </el-button-group>
          </right>
        </elm-row>
      </template>
      <elm-table :data="tableData" :page-size="9999" height="100%" v-loading="loading" v-if="tableData">
        <el-table-column prop="name" label="销售批次" align="center"></el-table-column>
        <el-table-column prop="dict_mc" label="货物名称" align="center"></el-table-column>
        <el-table-column prop="unit" label="单位" align="center" width="50"></el-table-column>
        <el-table-column prop="quantity" label="销售数量" align="center"></el-table-column>
        <el-table-column prop="price" label="销售价格" align="center"></el-table-column>
        <el-table-column prop="tex" label="税率" align="center" width="50">
          <template v-slot="scope">
            {{scope.row.tex?scope.row.tex+"%":""}}
          </template>
        </el-table-column>
        <el-table-column prop="in_price" label="进货额" align="center" :formatter="formatCurrency"></el-table-column>
        <el-table-column prop="in_tex" label="进项税" align="center" :formatter="formatCurrency"></el-table-column>
        <el-table-column prop="in_total" label="含税进货额" align="center" width="100" :formatter="formatCurrency"></el-table-column>
        <el-table-column prop="out_price" label="销售额" align="center" :formatter="formatCurrency"></el-table-column>
        <el-table-column prop="out_tex" label="销项税" align="center" :formatter="formatCurrency"></el-table-column>
        <el-table-column prop="out_total" label="含税销售额" align="center" width="100" :formatter="formatCurrency"></el-table-column>
        <el-table-column prop="profit" label="毛利润" align="center" :formatter="formatCurrency"></el-table-column>
        <el-table-column prop="difference_tex" label="差进项税" align="center" :formatter="formatCurrency"></el-table-column>
        <el-table-column fixed="right" label="操作" width="80px" align="center">
          <template v-slot="scope">
            <elm-open-button url="profit_stage" title="查看详情"  width="80%" height="60%" :data="{ outData: scope.row }" type="text">查看详情</elm-open-button>
          </template>
        </el-table-column>
      </elm-table>
    </elm-page-tb>
  </div>

</template>

<script>
export default {
  extends: elmVueBase,
  data() {
    return {
      postData: {
        name: null,
        startDate:new Date(new Date().setDate(1)).toISOString().split('T')[0],
        endDate: new Date().toISOString().split('T')[0],
      },
      tableData:[],
      loading:false
    }
  },
  async created() {
    await this.getTableData();
  },
  methods: {
    async getTableData(){
      this.loading=true;
      let res=await elm.post("/business/jxc/goods/out/GetData",this.postData);
      this.tableData=[];
      for(let i in res.pagedata){
        let item=res.pagedata[i];
        let re=await elm.post("/business/jxc/goods/out/stage/GetData", {out_id:item.id});
        item.in_price=0;
        item.in_tex=0;
        for(let it in re){
          item.in_price+=(re[it].quantity||0)*(re[it].price||0);
          item.in_tex+=(re[it].quantity||0)*(re[it].price||0)*(re[it].tex||0)/100;
        }
        item.in_total=item.in_price+item.in_tex;
        item.out_price=(item.price||0)*(item.quantity||0);
        item.out_tex=item.out_price*(item.tex||0)/100;
        item.out_total=item.out_price+item.out_tex;
        item.profit=item.out_price-item.in_total;
        item.difference_tex=item.out_tex-item.in_tex;
        this.tableData.push({...item});
      }
      this.loading=false;
    },
    formatCurrency(row, column, cellValue, index) {
      if (cellValue == null) return '0';
      return Number(parseFloat(cellValue).toFixed(2));
    }
  }
}
</script>
